<?php
class Application_Model_DbTable_Product extends Zend_Db_Table
{
    protected $_name = 'products';

    public function getProducts($page, $order) {
        $select = $this->select();

        if (true === is_array($order)) {
            $select->order($order);
        }

        if (null !== $page) {
            $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
            $count = clone $select;
            $count->reset(Zend_Db_Select::COLUMNS);
            $count->reset(Zend_Db_Select::FROM);
            $count->from($this->_name, new Zend_Db_Expr('COUNT(*) AS `zend_paginator_row_count`'));
            $adapter->setRowCount($count);

            $paginator = new Zend_Paginator($adapter);
            $paginator->setItemCountPerPage(2)
                      ->setCurrentPageNumber((int) $page);
            return $paginator;
        }
        return $this->fetchAll($select);
    }

    public function getMaxSort($categoryId)
    {
        $tableName = $this->_db->quoteIdentifier($this->_name);
        $sql = "
            SELECT MAX(sort_id) 
            FROM $tableName 
            WHERE $tableName.category_id='$categoryId'
        ";
        $maxSortId = $this->_db->fetchOne($sql);
        return $maxSortId;
    }
}